package com.link.base.base.saleactivity.controller;


import com.link.base.user.model.CoreUser;
import com.link.base.base.accnt.model.Account;
import com.link.base.base.accnt.service.AccountService;
import com.link.base.base.saleactivity.model.Odds;
import com.link.base.base.saleactivity.model.RaffleDetail;
import com.link.base.base.saleactivity.model.SaleActivity;
import com.link.base.base.saleactivity.service.OddsService;
import com.link.base.base.saleactivity.service.RaffleDetailService;
import com.link.base.base.saleactivity.service.SaleActivityService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.DateUtil;
import com.link.core.util.StringUtils;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author wangdawei
 * @date 2018/6/10
 * @description: 营销活动-抽奖概率Controller
 */

@Controller
@RequestMapping("/link/odds")
public class OddsController extends BasicController<Odds> {

    @Resource
    private OddsService oddsService;

    @Resource
    private RaffleDetailService raffleDetailService;

    @Resource
    private AccountService accountService;

    @Resource
    private SaleActivityService saleActivityService;

    @Override
    public BasicService<Odds> getBasicService() throws Exception {
        return oddsService;
    }

    /**
     * @description: 根据活动id 获取活动下抽奖的奖品信息,对应转盘6个位置上的奖品信息。
     * @author: wangdawei
     * @param saleActivity
     * @date: 2018/6/20
     */
    @RequestMapping(value = "/reffleInfo")
    @ResponseBody
    public Map<String, Object> queryActivityAndCouponsList(@JsonParam QueryParams qp, @JsonParam SaleActivity saleActivity, HttpSession session,
                                                           HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        Long activityId = saleActivity.getId();
        Long corpid = saleActivity.getCorpid();
        if (activityId == null) {
            activityId = Long.parseLong(request.getParameter("activityId"));
        }
        if (corpid == null) {
            corpid = Long.parseLong(request.getParameter("corpid"));
        }
        try {
            Odds odds = new Odds();
            odds.setActivityId(activityId);
            odds.setPageFlag(false);
            odds.setCorpid(corpid);
            odds.setLoginCorpId(corpid);
            List<Odds> oddsList = oddsService.queryByExamplePage(odds);
            for (int i = 0; oddsList != null && i < oddsList.size(); i++) {
                oddsList.get(i).setAnonymousFlag("Y");
                if ("Virtual".equals(oddsList.get(i).getCouponsClass())) {
                    oddsList.get(i).setCouponsName("谢谢惠顾");
                }
                if (oddsList.get(i).getLocation() == null) {
                    oddsList.remove(i);
                }
            }
            CoreUser user = UserUtil.getUser();
            if (user != null && BasicService.MP.equals(user.getLoginType())) {
                RaffleDetail raffleDetail = new RaffleDetail();
                raffleDetail.setActivityId( saleActivity.getId());
                raffleDetail.setAccntId(user.getId());
                raffleDetail.setCorpid(user.getCorpid());
                raffleDetail.setLoginCorpId(user.getCorpid());
                //查看日抽奖次数
                List<RaffleDetail> dayTimeList = raffleDetailService.queryByExamplePage(raffleDetail);
                result.put("remainTime", dayTimeList.size());
            }
            result.put("success", true);
            result.put("rows", oddsList);
        } catch (ServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<SaleActivity>());
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getMessage());
        }
        return result;
    }

    /**
     * @author: wangdawei
     * @param saleActivity
     * @date: 2018/6/20
     * @description: 获取大转盘抽奖消息。
     */
    @RequestMapping(value = "/smartScreenReffleInfo")
    @ResponseBody
    public Map<String, Object> smartScreenReffleInfo(@JsonParam QueryParams qp,@JsonParam SaleActivity saleActivity, HttpSession session,
                                                     HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        Long activityId = saleActivity.getId();
        Long corpid = saleActivity.getCorpid();
        saleActivity = saleActivityService.queryById(saleActivity);
        try {
            Odds odds = new Odds();
            odds.setActivityId(activityId);
            odds.setPageFlag(false);
            odds.setCorpid(corpid);
            odds.setLoginCorpId(corpid);
            List<Odds> oddsList = oddsService.queryByExamplePage(odds);
            for (int i = 0; oddsList != null && i < oddsList.size(); i++) {
                oddsList.get(i).setAnonymousFlag("Y");
                if ("Virtual".equals(oddsList.get(i).getCouponsClass())) {
                    oddsList.get(i).setCouponsName("谢谢惠顾");
                }
                if (oddsList.get(i).getLocation() == null) {
                    oddsList.remove(i);
                }
            }
            CoreUser user = UserUtil.getUser();
            RaffleDetail raffleDetail = new RaffleDetail();
            raffleDetail.setActivityId( saleActivity.getId());
            raffleDetail.setCorpid(corpid);
            raffleDetail.setLoginCorpId(corpid);
            raffleDetail.setRaffleTime(DateUtil.dateToStr(new Date(),"yyyy-MM-dd"));
            Account account = new Account();
            account.setCorpid(corpid);
            account.setLoginCorpId(corpid);
            account.setId(user.getId());
            account = accountService.queryById(account);
            if (StringUtils.isNotBlank(account.getMobilePhone1())) {
                raffleDetail.setAttr6(account.getMobilePhone1());
            } else {
                raffleDetail.setAccntId(user.getId());
            }
            //查看日抽奖次数
            List<RaffleDetail> dayTimeList = raffleDetailService.queryByExamplePage(raffleDetail);
            Integer remainTime = saleActivity.getDaywinTime() - dayTimeList.size();
            result.put("remainTime", remainTime);
            result.put("reffleTime", dayTimeList.size());
            result.put("success", true);
            result.put("rows", oddsList);
        } catch (ServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<SaleActivity>());
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getMessage());
        }
        return result;
    }

}
